package edu.ntnu.superrss.utils;

import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import edu.ntnu.superrss.models.SyndFeedImpl;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Comparators for soriting SyndFeedImpl.
 * @author Sigurd Fosseng <sigurd at fosseng.net>
 */
public class SyndFeedSort {
        /**
         * Comparator that sorts SyndFeeds by Date
         */
	public static final Comparator<SyndFeed> DATE_ORDER = new Comparator<SyndFeed>() {
		public int compare(SyndFeed t, SyndFeed t1) {
                    List tel = t.getEntries();
                    List t1el = t1.getEntries();
                    Collections.sort(tel,SyndEntrySort.DATE_ORDER);
                    Collections.sort(t1el, SyndEntrySort.DATE_ORDER);
                    SyndEntry te = (SyndEntry) tel.get(0);
                    SyndEntry t1e = (SyndEntry) t1el.get(0);
                    if(te==null) return -1;
                    if(t1e == null) return 1;
                    
                    return t1e.getPublishedDate().compareTo(te.getPublishedDate());
		}
	};
        /**
         * Comparator that sorts SyndFeeds by title
         */
	public static final Comparator<SyndFeed> TITLE_ORDER = new Comparator<SyndFeed>(){
		public int compare(SyndFeed t, SyndFeed t1) {
			return t.getTitle().compareTo(t1.getTitle());
		}
	};
        /**
         * Comparator that sorts SyndFeedImpl by the ID field
         */
	private static final Comparator<SyndFeedImpl> ID_ORDER = new Comparator<SyndFeedImpl>() {
		public int compare(SyndFeedImpl t, SyndFeedImpl t1) {
			return t.getId().compareTo(t1.getId());
		}
	};
}
